package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.dtos.ChannelDTO;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.pojos.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<AdChannelMapper , AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        if (dto ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        Page  page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName()).orderByAsc(AdChannel::getOrd);
        }

        IPage result = page(page,wrapper);

        PageResponseResult result1 = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(),result.getRecords());

        return result1;

    }

    //新增频道
    @Override
    public ResponseResult insert(AdChannel channel) {
        if (channel == null || StringUtils.isBlank(channel.getName())){
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道参数不能为空");
        }
        if (channel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数长度不等大于10");
        }

        //查询数量
        int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));

        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已经存在");
        }

        //保存频道
        channel.setCreatedTime(new Date());
        save(channel);

        return ResponseResult.okResult();
    }


    //修改频道
    @Override
    public ResponseResult update(AdChannel channel) {

        if (channel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"ID不能为空");
        }

        AdChannel adChannel = getById(channel.getId());
        if (adChannel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道名称不存在");
        }

        if (StringUtils.isNotBlank(channel.getName()) &&!channel.getName().equals(adChannel.getName())){
            int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
            if (count >0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已经存在");
            }
        }

        updateById(channel);
        return ResponseResult.okResult();
    }

    //删除
    @Transactional
    @Override
    public ResponseResult deleteById(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不能为空");
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道有效不能删除");
        }
        //3.删除频道
        removeById(id);

        if (id ==46){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id等于46不能删除");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

}
